"""Peewee migrations: ::

    > Model = migrator.orm['name']

    > migrator.sql(sql)
    > migrator.python(func, *args, **kwargs)
    > migrator.create_model(Model)
    > migrator.remove_model(Model, cascade=True)
    > migrator.add_fields(Model, **fields)
    > migrator.change_fields(Model, **fields)
    > migrator.remove_fields(Model, *field_names, cascade=True)
    > migrator.rename_field(Model, old_field_name, new_field_name)
    > migrator.rename_table(Model, new_table_name)
    > migrator.add_index(Model, *col_names, unique=False)
    > migrator.drop_index(Model, index_name)
    > migrator.add_not_null(Model, field_name)
    > migrator.drop_not_null(Model, field_name)
    > migrator.add_default(Model, field_name, default)

"""

import datetime as dt
import peewee as pw


def migrate(migrator, database, fake=False, **kwargs):
    """Write your migrations here."""

    @migrator.create_model
    class Country(pw.Model):
        id = pw.CharField(max_length=80, primary_key=True)
        screen_name = pw.CharField(max_length=100)
        logo = pw.CharField(max_length=200)

    @migrator.create_model
    class Currency(pw.Model):
        id = pw.CharField(max_length=255, primary_key=True)
        screen_name = pw.CharField(max_length=30)
        logo = pw.CharField(max_length=200, null=True)
        type = pw.CharField(default='normal', max_length=20)
        prefix = pw.CharField(max_length=10)
        suffix = pw.CharField(max_length=10, null=True)
        rate = pw.DecimalField(auto_round=False, decimal_places=2, default=1, max_digits=10, rounding='ROUND_HALF_EVEN')

    @migrator.create_model
    class Account(pw.Model):
        id = pw.CharField(max_length=60, primary_key=True)
        currency = pw.ForeignKeyField(db_column='currency_id', rel_model=Currency, to_field='id')
        balance = pw.DecimalField(auto_round=False, decimal_places=4, default=0.0, max_digits=10, rounding='ROUND_HALF_EVEN')
        frozen_amount = pw.DecimalField(auto_round=False, decimal_places=4, default=0.0, max_digits=10, rounding='ROUND_HALF_EVEN')
        pending_amount = pw.DecimalField(auto_round=False, decimal_places=4, default=0.0, max_digits=10, rounding='ROUND_HALF_EVEN')
        status = pw.CharField(default='normal', max_length=10)
        remark = pw.CharField(default='', max_length=300)
        updated_at = pw.DateTimeField()
        created_at = pw.DateTimeField()
        reward_amount = pw.DecimalField(auto_round=False, decimal_places=4, default=0.0, max_digits=10, rounding='ROUND_HALF_EVEN')

    @migrator.create_model
    class AccountHistory(pw.Model):
        account = pw.ForeignKeyField(db_column='account_id', rel_model=Account, to_field='id')
        direction = pw.CharField(default='debit', max_length=10)
        amount = pw.DecimalField(auto_round=False, decimal_places=4, max_digits=10, rounding='ROUND_HALF_EVEN')
        remark = pw.CharField(max_length=200)
        type = pw.CharField(max_length=20)

    @migrator.create_model
    class DeliverInfo(pw.Model):
        type = pw.CharField(max_length=255)
        carrier = pw.CharField(db_column='carriers', default='SYSTEM', max_length=200)
        trace_no = pw.CharField(max_length=80)
        extension = pw.CharField(max_length=400)
        deliver_time = pw.DateTimeField()
        status = pw.CharField(default='created', max_length=255)
        trace_info = pw.CharField(max_length=2000)

    @migrator.create_model
    class Member(pw.Model):
        credit_rating = pw.IntegerField(default=0)
        screen_name = pw.CharField(max_length=40)
        head_image = pw.CharField(max_length=400)
        sales_rate = pw.DecimalField(auto_round=False, db_column='sale_rate', decimal_places=1, default=0.0, max_digits=10, rounding='ROUND_HALF_EVEN')
        buyer_rate = pw.DecimalField(auto_round=False, decimal_places=1, default=0, max_digits=10, rounding='ROUND_HALF_EVEN')
        deal_count = pw.IntegerField(default=0)
        created_at = pw.DateTimeField()
        updated_at = pw.DateTimeField()
        country = pw.ForeignKeyField(db_column='country_id', rel_model=Country, to_field='id')
        currency = pw.ForeignKeyField(db_column='currency_id', rel_model=Currency, to_field='id')
        account = pw.ForeignKeyField(db_column='account_id', rel_model=Account, to_field='id')
        secret_key = pw.CharField(max_length=64)
        first_name = pw.CharField(max_length=255, null=True)
        last_name = pw.CharField(max_length=255, null=True)
        grade = pw.CharField(default='male', max_length=255)
        phone = pw.CharField(max_length=255, null=True)
        email = pw.CharField(max_length=255, null=True)
        status = pw.CharField(default='normal', max_length=20)

    @migrator.create_model
    class AuthHistory(pw.Model):
        member = pw.ForeignKeyField(db_column='member_id', rel_model=Member, to_field='id')
        created_at = pw.DateTimeField()
        login_ip = pw.CharField(max_length=20, null=True)
        country = pw.CharField(max_length=40, null=True)
        city = pw.CharField(max_length=60)
        platform = pw.CharField(default='web', max_length=20)
        platform_identify = pw.CharField(max_length=600, null=True)
        status = pw.CharField(default='normal', max_length=10)
        access_token = pw.CharField(max_length=60)

    @migrator.create_model
    class LocalAuth(pw.Model):
        login_name = pw.CharField(max_length=120)
        password = pw.CharField(max_length=120)
        retry_count = pw.IntegerField(default=0)
        created_at = pw.DateTimeField()
        updated_at = pw.DateTimeField()
        last_retry_time = pw.DateTimeField(null=True)
        member = pw.ForeignKeyField(db_column='member_id', rel_model=Member, to_field='id')


    @migrator.create_model
    class Order(pw.Model):
        description = pw.CharField(db_column='desc', max_length=200)
        remark = pw.CharField(max_length=450, null=True)
        member = pw.ForeignKeyField(db_column='member_id', rel_model=Member, to_field='id')
        status = pw.CharField(default='created', max_length=20)
        review_of_seller = pw.CharField(default='N', max_length=1)
        review_of_buyer = pw.CharField(default='N', max_length=1)
        fee = pw.DecimalField(auto_round=False, decimal_places=2, default=0.0, max_digits=10, rounding='ROUND_HALF_EVEN')
        real_fee = pw.DecimalField(auto_round=False, decimal_places=2, default=0.0, max_digits=10, rounding='ROUND_HALF_EVEN')
        real_amount = pw.DecimalField(auto_round=False, decimal_places=2, default=0.0, max_digits=10, rounding='ROUND_HALF_EVEN')
        amount = pw.DecimalField(auto_round=False, decimal_places=2, default=0.0, max_digits=10, rounding='ROUND_HALF_EVEN')
        discount_amount = pw.DecimalField(auto_round=False, decimal_places=2, default=0.0, max_digits=10, rounding='ROUND_HALF_EVEN')
        discount_method = pw.CharField(default='', max_length=30)
        discount_code = pw.CharField(db_column='discount_count', default='', max_length=80)
        payment_method = pw.CharField(default='', max_length=60)
        created_at = pw.DateTimeField()
        updated_at = pw.DateTimeField()
        payment_time = pw.DateTimeField(null=True)
        expire_time = pw.DateTimeField(null=True)
        settle_time = pw.DateTimeField()
        currency = pw.ForeignKeyField(db_column='currency_id', rel_model=Currency, to_field='id')
        trade_code = pw.CharField(max_length=255)
        payment_seq = pw.CharField(max_length=255, null=True)
        notify_info = pw.CharField(max_length=400, null=True)

    @migrator.create_model
    class ProductCategory(pw.Model):
        id = pw.CharField(max_length=40, primary_key=True)
        screen_name = pw.CharField(max_length=255)

    @migrator.create_model
    class Product(pw.Model):
        id = pw.CharField(max_length=50, primary_key=True)
        title = pw.CharField(max_length=255)
        sub_title = pw.CharField(max_length=200, null=True)
        type = pw.CharField(default='game', max_length=20)
        status = pw.CharField(default='normal', max_length=20)
        created_at = pw.DateTimeField()
        updated_at = pw.DateTimeField(null=True)
        cover = pw.CharField(max_length=400, null=True)
        like_count = pw.IntegerField(default=0)
        click_count = pw.IntegerField(default=0)
        collection_count = pw.IntegerField(default=0)
        mask = pw.CharField(max_length=32, null=True)
        category = pw.ForeignKeyField(db_column='category_id', rel_model=ProductCategory, to_field='id')
        structure = pw.CharField(max_length=80, null=True)

    @migrator.create_model
    class Offer(pw.Model):
        status = pw.CharField(default='normal', max_length=255)
        count = pw.IntegerField(default=0)
        sales_count = pw.IntegerField(default=0)
        slug = pw.CharField(max_length=20, null=True)
        product = pw.ForeignKeyField(db_column='product_id', rel_model=Product, to_field='id')
        member = pw.ForeignKeyField(db_column='member_id', rel_model=Member, to_field='id')
        price = pw.DecimalField(auto_round=True, decimal_places=4, max_digits=15, rounding='ROUND_HALF_EVEN')
        mask = pw.CharField(default='', max_length=80)
        created_at = pw.DateTimeField()
        updated_at = pw.DateTimeField()

    @migrator.create_model
    class OrderDetail(pw.Model):
        order = pw.ForeignKeyField(db_column='order_id', rel_model=Order, to_field='id')
        count = pw.IntegerField()
        amount = pw.DecimalField(auto_round=False, decimal_places=2, default=0.0, max_digits=10, rounding='ROUND_HALF_EVEN')
        real_amount = pw.DecimalField(auto_round=False, decimal_places=2, default=0.0, max_digits=10, rounding='ROUND_HALF_EVEN')
        product = pw.ForeignKeyField(db_column='product_id', rel_model=Product, to_field='id')
        offer = pw.ForeignKeyField(db_column='offer_id', rel_model=Offer, to_field='id')
        discount_amount = pw.DecimalField(auto_round=False, decimal_places=2, default=0.0, max_digits=10, rounding='ROUND_HALF_EVEN')
        discount_method = pw.CharField(default='', max_length=30)
        discount_code = pw.CharField(db_column='discount_count', default='', max_length=80)
        delivery_method = pw.CharField(default='ticket', max_length='80')
        deliver_info = pw.ForeignKeyField(db_column='deliver_info_id', null=True, rel_model=DeliverInfo, to_field='id')

    @migrator.create_model
    class OrderDetailExt(pw.Model):
        order_detail = pw.ForeignKeyField(db_column='order_detail_id', rel_model=OrderDetail, to_field='id')
        count = pw.IntegerField()
        amount = pw.DecimalField(auto_round=False, decimal_places=5, max_digits=10, rounding='ROUND_HALF_EVEN')
        ext_code = pw.CharField(max_length=40)
        ext_type = pw.CharField(max_length=40)

    @migrator.create_model
    class Metadata(pw.Model):
        id = pw.CharField(max_length=40, primary_key=True)
        screen_name = pw.CharField(max_length=255)
        product_category = pw.ForeignKeyField(db_column='product_category_id', rel_model=ProductCategory, to_field='id')
        value_mode = pw.CharField(default='text', max_length=255)
        extension = pw.TextField(null=True)

    @migrator.create_model
    class ProductContent(pw.Model):
        language = pw.CharField(max_length=10)
        product_id = pw.CharField(max_length=255)
        summary = pw.CharField(max_length=400, null=True)
        detail = pw.TextField()

    @migrator.create_model
    class ProductMetadata(pw.Model):
        product = pw.ForeignKeyField(db_column='product_id', rel_model=Product, to_field='id')
        metadata = pw.ForeignKeyField(db_column='metadata_id', rel_model=Metadata, to_field='id')
        value = pw.CharField(max_length=400)

    @migrator.create_model
    class Service(pw.Model):
        id = pw.CharField(max_length=40, primary_key=True)
        screen_name = pw.CharField(max_length=255)
        short_name = pw.CharField(max_length=255)
        fee = pw.DecimalField(auto_round=False, decimal_places=2, max_digits=10, rounding='ROUND_HALF_EVEN')
        mode = pw.CharField(default='fixed', max_length=255)
        created_at = pw.DateTimeField()
        updated_at = pw.DateTimeField()

    @migrator.create_model
    class ShopCart(pw.Model):
        id = pw.CharField(max_length=60, primary_key=True)
        member = pw.ForeignKeyField(db_column='member_id', null=True, rel_model=Member, to_field='id')
        created_at = pw.DateTimeField()
        updated_at = pw.DateTimeField()
        status = pw.CharField(default='created', max_length=20)

    @migrator.create_model
    class CartItem(pw.Model):
        product = pw.ForeignKeyField(db_column='product_id', rel_model=Product, to_field='id')
        stock = pw.ForeignKeyField(db_column='stock_id', rel_model=Offer, to_field='id')
        shop_cart = pw.ForeignKeyField(db_column='shop_cart_id', null=True, rel_model=ShopCart, to_field='id')
        count = pw.IntegerField(default=0)
        created_at = pw.DateTimeField()
        status = pw.CharField(default='normal', max_length=20)
        source = pw.CharField(max_length=100)

    @migrator.create_model
    class CartItemAdditive(pw.Model):
        title = pw.CharField(max_length=80)
        description = pw.CharField(max_length=200, null=True)
        type = pw.CharField(max_length=20)
        ref_id = pw.CharField(max_length=80)
        required = pw.CharField(default='Y', max_length=1)
        cart_item = pw.ForeignKeyField(db_column='cart_item_id', rel_model=CartItem, to_field='id')





def rollback(migrator, database, fake=False, **kwargs):
    """Write your rollback migrations here."""

    migrator.remove_model('cartitemadditive')

    migrator.remove_model('cartitem')

    migrator.remove_model('shopcart')

    migrator.remove_model('service')

    migrator.remove_model('productmetadata')

    migrator.remove_model('productcontent')

    migrator.remove_model('metadata')

    migrator.remove_model('orderdetailext')

    migrator.remove_model('orderdetail')

    migrator.remove_model('offer')

    migrator.remove_model('product')

    migrator.remove_model('productcategory')

    migrator.remove_model('order')

    migrator.remove_model('modelmixin')

    migrator.remove_model('localauth')

    migrator.remove_model('authhistory')

    migrator.remove_model('member')

    migrator.remove_model('deliverinfo')

    migrator.remove_model('accounthistory')

    migrator.remove_model('account')

    migrator.remove_model('currency')

    migrator.remove_model('country')
